package cn.gavin.web.controller;

import cn.gavin.domain.Employee;
import cn.gavin.qo.EmployeeQueryObject;
import cn.gavin.qo.JsonResult;
import cn.gavin.service.IDepartmentService;
import cn.gavin.service.IEmployeeService;
import cn.gavin.service.IRoleService;
import cn.gavin.util.RequiredPermission;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequiredPermission(name = "员工页面",expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo){
        // 获取分页查询的数据，存到 Model 中
        model.addAttribute("pageInfo",employeeService.query(qo));
        model.addAttribute("departments",departmentService.listAll()); // 查询所有部门模型存入，让员工列表页面显示供用户选择
        return "employee/list";
    }

    @RequiredPermission(name = "员工删除",expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            employeeService.deleteById(id);
        }
        return "redirect:/employee/list";
    }

    @RequiredPermission(name = "员工新增/编辑",expression = "employee:save")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            Employee employee = employeeService.findById(id);
            model.addAttribute("employee",employee);
        }
        model.addAttribute("roles",roleService.listAll());
        model.addAttribute("departments",departmentService.listAll());
        return "employee/input";
    }

    @RequiredPermission(name = "员工新增/编辑",expression = "employee:save")
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult save(Employee employee,Long[] ids){
        if (employee.getId() != null) {
            employeeService.update(employee,ids);
        }else {
            employeeService.insert(employee,ids);
        }
        return new JsonResult();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String username){
        Map<String, Boolean> map = new HashMap<>();
        Employee employee = employeeService.selectByUsername(username);
        // 判断用户名是否为空，为空则返回
        map.put("valid",employee == null);
        return map;
    }

    @RequiredPermission(name = "员工导出",expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        // 文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook wb = employeeService.exportXls();
        // 把 excel 的数据输出到浏览器
        wb.write(response.getOutputStream());
    }

    @RequiredPermission(name = "员工导入",expression = "employee:importXls")
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeService.importXls(file);
        return new JsonResult();
    }
}
